
*** Dataset: 
use "$data\4_individual_ano_vars.dta", clear
merge m:1 id wave using  "$data\2_firm_regressions_individual_paper.dta", keepusing(empretention_2021 empcat)

ren empretention_2021 empretention_raw_2021

	keep if _merge == 3
	replace empretention_raw_2021=0 if empretention_raw_2021==.
	gen 	left_firm_raw_2021 = 1 - empretention_raw_2021

	*Standarize variable
	egen 		left_firm_2021 = std(left_firm_raw_2021) 
	replace 	left_firm_2021 = 0 if missing(left_firm_raw_2021)
	gen 		missing_left_firm_2021 = 0
	replace 	missing_left_firm_2021 = 1 if missing(left_firm_raw_2021)


	*Sample Restriction
	keep 	if employer==0 & inlist(status,1,3, 4)



	drop 	if wave == 1
	bys		id_indiv: egen n=count(id_indiv)
	drop	if wave == 2 & n==2

	*Retention Variable

	gen 	left_firm = 1 if status == 3
	replace left_firm = 0 if status == 1
	replace left_firm = 0 if status == 4

	ren 	assignment Treat
	ren 	abidjan_base Abidjan
	replace Abidjan = 2 if Abidjan == 0 
	label 	define Abidjan 2 "Outside Abidjan" 1 "In Abidjan"
	label 	val Abidjan Abidjan

	ren 	empcat Number_Staff
	label 	define Number_Staff 1 "1-3 employees" 2 "4-6 employees" 3 "6+ employees"
	label	val Number_Staff Number_Staff

	global 		outcome left_firm
	estimate 	clear 

	local j=1 
	foreach h of varlist Abidjan {
	foreach o of global outcome {
	
		ta `h'
		local cats=`r(r)'
	
		* Run the regression
		areg  `o' i.Treat##i.`h' educ_sec i.ag_ano  left_firm_2021 missing_left_firm_2021 i.wave, a(strata_abidjan_alt) vce(cl id) 

		* Run margins for the interaction term
		eststo m_`h': margins `h', dydx(1.Treat) post coeflegend
		// TE for subgroups and test for significance of interactions 
		test _b[1.Treat:1.`h'] = _b[1.Treat:2.`h']
		local ${tbl}_p`j'=round(r(p),.001)
	
			
		forv i=1/`cats' {
		di "Save N and control group means in macro to include in table" 
			sum `o' if `h'==`i' & e(sample)
			local ${tbl}_n`j'_`i'=r(N)

			sum `o' if Treat==0 & `h'==`i' & e(sample)
			local ${tbl}_cm`j'_`i'=round(r(mean),.001)
			}
	

		
	if `cats'==3 {
		di "3 Categories: test for differences in treatment effects"
	*** Significant difference between treatment effects? 
		// 2 vs. 1 and 3 vs. 1 are simply the coefficients of the interaction terms
		test _b[1.Treat:2.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_12=round(r(p),.001)
		di "P-value of 1 vs. 2: `${tbl}_p`j'_12'"
		
		test _b[1.Treat:3.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_13=round(r(p),.001)
		di "P-value of 1 vs. 3: `${tbl}_p`j'_13'"
		
		// 2 vs. 3 is test for significant difference in coefficients of interaction terms 
		test _b[1.Treat:2.`h'] = _b[1.Treat:3.`h'] 
		local ${tbl}_p`j'_23=round(r(p),.001)
		di "P-value of 2 vs. 3: `${tbl}_p`j'_23'"
	}

	*** Case of 2 categories 
	if `cats'==2 {
		di "2 categories: test for differences in treatment effects"
		// 2 vs. 1 coefficients of the interaction term
		test _b[1.Treat:1.`h'] = _b[1.Treat:2.`h']
		local ${tbl}_p`j'_12=round(r(p),.001)
		di "P-value of 1 vs. 2: `${tbl}_p`j'_12'"

	}
	
	if `cats'==2 {
		
		estadd 	local 		N_1_`j' `${tbl}_n`j'_1'
		local 	cm_1 	= 	string(`${tbl}_cm`j'_1', "%9.3f")
		estadd 	local 		cm_1_`j' `cm_1'
		
		
		estadd local N_2_`j'  `${tbl}_n`j'_2'
		local cm_2 = string(`${tbl}_cm`j'_2', "%9.3f")
		estadd local cm_2_`j' `cm_2' 
		
		local interaction_12 = string(`${tbl}_p`j'_12', "%9.3f")
		estadd local interaction_12_`j' `interaction_12'
		
		}

	if `cats'==3 {
		estadd 	local 		N_1_`j' `${tbl}_n`j'_1'
		local 	cm_1 	= 	string(`${tbl}_cm`j'_1', "%9.3f")
		estadd 	local 		cm_1_`j' `cm_1'
		
		
		estadd local N_2_`j'  `${tbl}_n`j'_2'
		local cm_2 = string(`${tbl}_cm`j'_2', "%9.3f")
		estadd local cm_2_`j' `cm_2' 
		
		estadd local N_3_`j'  `${tbl}_n`j'_3'
		local cm_3 = string(`${tbl}_cm`j'_3', "%9.3f")
		estadd local cm_3_`j' `cm_3' 

		local interaction_12 = string(`${tbl}_p`j'_12', "%9.3f")
		local interaction_13 = string(`${tbl}_p`j'_13', "%9.3f")
		local interaction_23 = string(`${tbl}_p`j'_23', "%9.3f")

		
		estadd local interaction_12_`j' `interaction_12'
		estadd local interaction_13_`j' `interaction_13'
		estadd local interaction_23_`j' `interaction_23'
	}	
		

		
		
		esttab m* using "$results\01_tables\Table_S21_retention_hetero.tex", ///
		keep(1.Treat:1.`h') ///
		scalars("cm_1_`j' Mean" "N_1_`j' N") ///
		star(* 0.10 ** 0.05 *** 0.01) ///
		se sfmt(%9.3f) noobs  ///
		mtitle("Left Firm") ///
		prehead("\begin{tabular}{l*{10}{c}}\hline\hline" ///
			"\multicolumn{1}{l}{\textbf{Panel A: Location}} \\"  ) ///
		prefoot("\hline \\") ///
		postfoot("") ///
		replace
		
	

		
		esttab m_* using "$results\01_tables\Table_S21_retention_hetero.tex", ///
		keep(1.Treat:2.`h') ///
		scalars("cm_2_`j' Mean"  "N_2_`j' N"  "interaction_12_`j' P-val Diff. between group 1 and 2" ) ///
		star(* 0.10 ** 0.05 *** 0.01) ///
		se sfmt(%9.3f) noobs  ///
		nonumbers nomtitles nonotes ///
		prehead("") ///
		prefoot("\hline") ///
		postfoot("") ///
		append
	
	
			
	}	

	}
	
estimates clear
local j=1 
foreach o of global outcome {
foreach h of varlist Number_Staff {
 	 local current_outcome = "`o'"  
    
	// Detect no. of categories (2 or dummy) 
		ta `h'
		local cats=`r(r)'

		* Regression
	
	
		areg  `o' i.Treat##i.`h' educ_sec i.ag_ano  left_firm_2021 missing_left_firm_2021 i.wave, a(strata_empcat_alt) vce(cl id) 
		eststo m_`o':	margins `h', dydx(1.Treat) post coeflegend
		


		// TE for subgroups and test for significance of interactions 
		test _b[1.Treat:1.`h'] = _b[1.Treat:2.`h']
		local ${tbl}_p`j'=round(r(p),.001)
	
			
		forv i=1/`cats' {
		di "Save N and control group means in macro to include in table" 
			sum `o' if `h'==`i' & e(sample)
			local ${tbl}_n`j'_`i'=r(N)

			sum `o' if Treat==0 & `h'==`i' & e(sample)
			local ${tbl}_cm`j'_`i'=round(r(mean),.001)
			}
		
	if `cats'==3 {
		di "3 Categories: test for differences in treatment effects"
	*** Significant difference between treatment effects? 
		// 2 vs. 1 and 3 vs. 1 are simply the coefficients of the interaction terms
		test _b[1.Treat:2.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_12=round(r(p),.001)
		di "P-value of 1 vs. 2: `${tbl}_p`j'_12'"
		
		test _b[1.Treat:3.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_13=round(r(p),.001)
		di "P-value of 1 vs. 3: `${tbl}_p`j'_13'"
		
		// 2 vs. 3 is test for significant difference in coefficients of interaction terms 
		test _b[1.Treat:2.`h'] = _b[1.Treat:3.`h'] 
		local ${tbl}_p`j'_23=round(r(p),.001)
		di "P-value of 2 vs. 3: `${tbl}_p`j'_23'"
	}

	*** Case of 2 categories 
	if `cats'==2 {
		di "2 categories: test for differences in treatment effects"
		// 2 vs. 1 coefficients of the interaction term
		test _b[1.Treat:1.`h'] = _b[1.Treat:2.`h']
		local ${tbl}_p`j'_12=round(r(p),.001)
		di "P-value of 1 vs. 2: `${tbl}_p`j'_12'"

	}
	
	if `cats'==2 {
		
		estadd 	local 		N_1_`j' `${tbl}_n`j'_1'
		local 	cm_1 	= 	string(`${tbl}_cm`j'_1', "%9.3f")
		estadd 	local 		cm_1_`j' `cm_1'
		
		
		estadd local N_2_`j'  `${tbl}_n`j'_2'
		local cm_2 = string(`${tbl}_cm`j'_2', "%9.3f")
		estadd local cm_2_`j' `cm_2' 
		
		local interaction_12 = string(`${tbl}_p`j'_12', "%9.3f")
		estadd local interaction_12_`j' `interaction_12'
		
		}

	if `cats'==3 {
		estadd 	local 		N_1_`j' `${tbl}_n`j'_1'
		local 	cm_1 	= 	string(`${tbl}_cm`j'_1', "%9.3f")
		estadd 	local 		cm_1_`j' `cm_1'
		
		
		estadd local N_2_`j'  `${tbl}_n`j'_2'
		local cm_2 = string(`${tbl}_cm`j'_2', "%9.3f")
		estadd local cm_2_`j' `cm_2' 
		
		estadd local N_3_`j'  `${tbl}_n`j'_3'
		local cm_3 = string(`${tbl}_cm`j'_3', "%9.3f")
		estadd local cm_3_`j' `cm_3' 

		local interaction_12 = string(`${tbl}_p`j'_12', "%9.3f")
		local interaction_13 = string(`${tbl}_p`j'_13', "%9.3f")
		local interaction_23 = string(`${tbl}_p`j'_23', "%9.3f")

		
		estadd local interaction_12_`j' `interaction_12'
		estadd local interaction_13_`j' `interaction_13'
		estadd local interaction_23_`j' `interaction_23'
        
		* Create table
        
        * First category (1-3 employees)
        esttab m* using "$results\01_tables\Table_S21_retention_hetero.tex", ///
            keep(1.Treat:1.`h') ///
            scalars("cm_1_`j' Mean" "N_1_`j' N") ///
            star(* 0.10 ** 0.05 *** 0.01) ///
            se sfmt(%9.3f) noobs ///
            mtitle("Left Firm") ///
            prehead("\multicolumn{1}{l}{\textbf{Panel B: Number of Staff}} \\") ///
            prefoot("\hline \\") ///
            postfoot("") ///
            append
            
        * Second category (4-6 employees)
        esttab m* using "$results\01_tables\Table_S21_retention_hetero.tex", ///
            keep(1.Treat:2.`h') ///
            scalars("cm_2_`j' Mean" "N_2_`j' N") ///
            star(* 0.10 ** 0.05 *** 0.01) ///
            se sfmt(%9.3f) noobs ///
            nonumbers nomtitles nonotes ///
			prehead("") ///
            prefoot("\hline \\") ///
            postfoot("") ///
            append
            
        * Third category (6+ employees)
        esttab m* using "$results\01_tables\Table_S21_retention_hetero.tex", ///
            keep(1.Treat:3.`h') ///
            scalars("cm_3_`j' Mean" "N_3_`j' N" ///
                   "interaction_12_`j' P-val Diff. Group 1 & 2" ///
                   "interaction_13_`j' P-val Diff. Group 1 & 3" ///
                   "interaction_23_`j' P-val Diff. Group 2 & 3") ///
            star(* 0.10 ** 0.05 *** 0.01) ///
            se sfmt(%9.3f) noobs ///
            nonumbers nomtitles nonotes ///
			prehead("") ///
            prefoot("\hline") ///
            postfoot("\hline\hline\end{tabular}") ///
            append
    }
}
}
